package com.probox.common.core.utils.money;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.IOException;

public class ShareImageUtil {

    public static BufferedImage zoomImage(BufferedImage bufImg, int w, int h) {
        BufferedImage result = null;
        double wr=0,hr=0;
        Image Itemp = bufImg.getScaledInstance(w, h, bufImg.SCALE_SMOOTH);//设置缩放目标图片模板
        wr=w*1.0/bufImg.getWidth();     //获取缩放比例
        hr=h*1.0 / bufImg.getHeight();

        AffineTransformOp ato = new AffineTransformOp(AffineTransform.getScaleInstance(wr, hr), null);
        Itemp = ato.filter(bufImg, null);
        result = (BufferedImage) Itemp;
        return result;
    }

    public static BufferedImage getImage(String requestUrl) {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        //InputStream is  = null;
        BufferedImage image = null;
        try {
            //创建Get请求，
            HttpGet httpGet = new HttpGet(requestUrl);
            //执行Get请求，
            response = httpClient.execute(httpGet);
            //得到响应体
            HttpEntity entity = response.getEntity();
            //is = entity.getContent();
            image = ImageIO.read(entity.getContent());
            httpClient.close();
        } catch (IOException e) {
        }
        return image;
    }



    public static BufferedImage createProductImage(BufferedImage buffImg,
                                                   BufferedImage qrcode,BufferedImage proImg, String prdouctName,String price)
            throws IOException {

        if (buffImg == null || qrcode == null) return null;

        // 获取底图
        Graphics2D g2d = buffImg.createGraphics();

        BufferedImage tmpImg = zoomImage(qrcode,172,172);
        int imgWidth = tmpImg.getWidth();// 获取层图的宽度
        int imgHeight = tmpImg.getHeight();// 获取层图的高度
        g2d.drawImage(tmpImg, 464, 668, imgWidth, imgHeight, null);

        tmpImg = zoomImage(proImg,588,588);
        imgWidth = tmpImg.getWidth();// 获取层图的宽度
        imgHeight = tmpImg.getHeight();// 获取层图的高度
        g2d.drawImage(tmpImg, 27, 27, imgWidth, imgHeight, null);

        Color textColor = new Color(78, 78, 78);
        g2d.setColor(textColor);
        Font font = new Font("黑体", Font.PLAIN, 30);
        g2d.setFont(font);
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        int top = 665;
        if (prdouctName.length()>26) {
            prdouctName = prdouctName.substring(0, 26);
        }
        String text = null;
        while(prdouctName.length()>13) {
            text = prdouctName.substring(0,13);
            g2d.drawString(text, 27, top);
            top += 40;
            prdouctName = prdouctName.substring(13);
        }
        if (prdouctName.length()>0)
            g2d.drawString(prdouctName, 27, top);

        textColor = new Color(192, 61, 53);
        g2d.setColor(textColor);
        font = new Font("黑体", Font.PLAIN, 54);
        g2d.setFont(font);
        g2d.drawString(price, 27, 770);


        textColor = new Color(158, 158, 158);
        g2d.setColor(textColor);
        font = new Font("黑体", Font.PLAIN, 24);
        g2d.setFont(font);
        g2d.drawString("长按识别二维码访问", 27, 816);


        g2d.dispose();// 释放图形上下文使用的系统资源
        return buffImg;
    }

}
